package com.lifetime.controller;

import com.lifetime.service.PicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


@RestController
@Controller
public class PicController {
    @Autowired
    PicService picService;

    @Configuration
    public class MyPicConfig implements WebMvcConfigurer {
        //addResourceHandler是指你想在url请求的路径
        // addResourceLocations是图片存放的真实路
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/getAllPic/**").addResourceLocations("file:E:\\PicLocation\\");
        }
    }


    @RequestMapping(value = "getAllPic", method = RequestMethod.GET)
    public Object AllPic(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
//   int userId=1;
        if (userId == 0) {
            return null;
        }
        return picService.getAllPic(userId);
    }

    /*InstallDescription*/
    @PostMapping(value = "InstallDescription")
    public int InstallDescription(@RequestBody Map<String, String> map,HttpServletRequest request) throws ParseException {
        int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        return picService.InstallDescription(map,userId);
    }

    /**
     * 获取所有的主题
     */
    @GetMapping("getAllObject")
    public Object getAllObject(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        return picService.getOneObject(null,userId );
    }

    /**
     * 获取其他所有的主题
     */
    @GetMapping("getOtherObject")
    public Object getOtherObject(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        return picService.getOtherObject(userId);
    }

    /**
     * 获取单独的主题
     */
    @GetMapping("getOneObject")
    public Object getOneObject(String i,HttpServletRequest request) {

        int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
        return picService.getOneObject(i, userId);
    }

    @PostMapping("deletePic")
    public void deletePic(@RequestBody int[] ids) {
        for (int id : ids) {
            picService.deletePic(id);
        }
    }

    @PostMapping("moveObject")
    public void moveObject(@RequestBody Object[] obj,HttpServletRequest request) {
        for (int i = 0; i < obj.length - 1; i++) {
            int userId = Integer.parseInt(request.getSession().getAttribute("userId").toString());
            picService.moveObject(Integer.parseInt(String.valueOf(obj[i])), String.valueOf(String.valueOf(obj[obj.length - 1])),userId);
        }
    }
}
